package com.gelapps.memogel;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

public class Functions {

    private final static String LCaseChars = "abcdefgijkmnopqrstwxyz";
    private final static String UCaseChars = "ABCDEFGHJKLMNPQRSTWXYZ";
    private final static String NumericChars = "23456789";
    private final static String SpecialChars = "*$-+?_&=!%";
	
	public static boolean partialAnd(boolean cond1, boolean cond2)
	{
		if (cond1) 
			return cond2;
		return cond1;
	}

	public static void showShortToast(Context con, String str)
	{
		Toast toast = Toast.makeText(con, str, Toast.LENGTH_SHORT);
		toast.show();
	}
	
	public static void showLongToast(Context con, String str)
	{
		Toast toast = Toast.makeText(con, str, Toast.LENGTH_LONG);
		toast.show();
	}

    public static String getStringOfFile(String path)
    {
        String ret = null;
        Log.i("Functions getStringOfFile", "File: "+ path);
        try{
            StringBuffer fileData = new StringBuffer(1000);
            BufferedReader reader = new BufferedReader(new FileReader(path));
            char[] buf = new char[1024];
            int numRead=0;
            while((numRead=reader.read(buf)) != -1){
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
            ret = fileData.toString();
        }
        catch(IOException e)
        {
            Log.e("getStringOfFile e", e.getMessage());
        }
        return ret;
    }

    public static boolean checkName(String name)
    {
        return isOkString(name);
    }

    public static boolean checkUser(String user)
    {
        return isOkString(user);
    }

    public static boolean checkPass(String pass)
    {
        return isOkString(pass);
    }

    public static boolean checkServer(String server)
    {
        return isOkString(server);
    }

    public static boolean isStringEmpty(String pass)
    {
        return pass.equals(Constants.getEmptyString());
    }

    public static boolean isOkCardNumber(String cardNumber)
    {
        if (cardNumber.length()!=16) return false;
        int pari=0, dispari=0;
        char[] number = cardNumber.toCharArray();
        for (int i=0;i<16;i++)
            if (i%2==0) {
                int p = Character.getNumericValue(number[i])*2;
                if (p>9)
                    pari =pari + p%10 + p/10;
                else
                    pari = pari + p;
            }
            else
                dispari = dispari + Character.getNumericValue(number[i]);
        if (((pari+dispari)%10)!=0) return false;
        return true;
    }


    public static boolean isOkCVV(String cvv)
    {
        if (cvv.length()==3) return true;
        return false;
    }

    public static boolean isOkPin(String pin)
    {
        if (pin.length()==5) return true;
        return false;
    }

    public static String genPass(int nChar, boolean number, boolean lChar, boolean uChar, boolean alpha)
    { //http://elblog.elbuild.it/2012/08/23/generare-password-random-in-java/
        String bag = "";
        if (number) bag = bag + NumericChars;
        if (lChar) bag = bag + LCaseChars;
        if (uChar) bag = bag + UCaseChars;
        if (alpha) bag = bag + SpecialChars;
        StringBuilder sb = new StringBuilder(nChar);
        Random rnd = new Random(System.currentTimeMillis());
        for (int i = 0; i < nChar; i++) {
            sb.append(bag.charAt(rnd.nextInt(bag.length())));
        }
        return sb.toString();
    }

    private static boolean isOkString(String toCheck)
    {
        if (toCheck==null) return false;
        if (toCheck.equals(Constants.getEmptyString())) return false;
        if (toCheck.length()<2) return false;
        return true;
    }
}
